const router = require('koa-router')()
const userService = require('../controllers/mysqlConfig');
const moment = require('moment')
//router.prefix('/users')

//获取所有用户(GET请求)
router.get('/user', async (ctx, next) => {
  let res = ctx.res
  ctx.body = {
    resultCode: 200,
    data: await userService.findUserData(),
    msg: '查询成功'
  }
})

//
router.post('/login', async (ctx, next) => {
  let reqData = ctx.request.body
  let data = await userService.login(reqData)
  if (data.length >= 0) {
    for (let i = 0; i < data.length; i++) {
      if (data[i].username === reqData.user) {
        ctx.body = {
          resultCode: 200,
          data: null,
          msg: '登录成功'
        }
        return
      } else {
        ctx.body = {
          resultCode: 545,
          data: null,
          msg: '登录失败'
        }
      }
    }
  } else {
    ctx.body = {
      resultCode: 545,
      data: null,
      msg: '无效用户'
    }
  }

})

// 增加用户(POST请求)
router.post('/register', async (ctx, next) => {
  let arr = [];

  arr.push(ctx.request.body['user']);
  arr.push(ctx.request.body['password']);

  await userService.addUserData(arr)
    .then((data) => {
      let r = '';
      if (data.affectedRows != 0) {
        r = '注册成功';
      }
      ctx.body = {
        resultCode: 200,
        data: null,
        msg: r
      }
    }).catch(() => {
      ctx.body = {
        resultCode: 500,
        data: null,
        msg: '注册失败'
      }
    })
})

router.get('/getMsgData', async (ctx, next) => {
  let arr = [];
  await userService.getMsgData()
    .then((data) => {
      ctx.body = {
        resultCode: 200,
        data: data,
        msg: '消息查询成功'
      }
    }).catch(() => {
      ctx.body = {
        resultCode: 500,
        data: null,
        msg: '消息查询失败'
      }
    })
})
router.post('/upPwd', async (ctx, next) => {
  let obj = {
    username: ctx.request.body['username'],
    password: ctx.request.body['password']
  };


  await userService.upPwd(obj)
    .then((data) => {

      console.log(data.changedRows !== 1, "dsdssd")
      ctx.body = {
        resultCode: data.changedRows !== 1 ? 500 : 200,
        data: null,
        msg: data.changedRows !== 1 ? '修改失败' : '修改成功'
      }
    }).catch(() => {
      ctx.body = {
        resultCode: 500,
        data: null,
        msg: '修改失败'
      }
    })
})




router.post('/addRegist', async (ctx, next) => {
  let obj = {
    username: ctx.request.body['username'],
    password: ctx.request.body['password'],
    registrationAddress: ctx.request.body['registrationAddress'],
    registrationTime: moment(ctx.request.body['registrationTime']).format('YYYY-MM-DD'),
    isAdmin: ctx.request.body['isAdmin'],
    birth: moment(ctx.request.body['birth']).format('YYYY-MM-DD'),
    phone: ctx.request.body['phone'],
    location: ctx.request.body['location'],
    gender: ctx.request.body['gender'],
  }
  await userService.addRegist(obj)
    .then((data) => {
      console.log(data)
      if (data.affectedRows > 0) {
        ctx.body = {
          resultCode: 200,
          data: null,
          msg: '新增成功'
        }
      } else {
        ctx.body = {
          resultCode: 200,
          data: null,
          msg: '新增失败'
        }
      }
    }).catch(() => {
      ctx.body = {
        resultCode: 500,
        data: null,
        msg: '失败'
      }
    })
})


router.get('/getUserTable', async (ctx, next) => {
  await userService.findUserData()
    .then((data) => {
      ctx.body = {
        resultCode: 200,
        data: data,
        msg: '用户查询成功'
      }
    }).catch(() => {
      ctx.body = {
        resultCode: 500,
        data: null,
        msg: '用户查询失败'
      }
    })
})



module.exports = router
